package com.wxx.service.tool.rong;

import com.google.common.collect.Maps;
import com.wxx.common.Const;
import com.wxx.util.AppUtil;
import com.wxx.util.PropertiesUtil;
import io.rong.RongCloud;
import io.rong.methods.group.Group;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.response.GroupUserQueryResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class RongServiceImpl implements RongService {

    /**
     * 此处替换成您的appKey
     * */
    private static final String appKey = PropertiesUtil.getProperty("RONG_APP_KEY");
    /**
     * 此处替换成您的appSecret
     * */
    private static final String appSecret = PropertiesUtil.getProperty("RONG_APP_SECRET");
    /**
     * 自定义api地址
     * */
    private static final String api = "http://api.cn.ronghub.com";

    RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
    //自定义 api 地址方式
    // RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret,api);

    Group Group = rongCloud.group;


    @Override
    public Map createGroup(String groupId, String groupName, String... userIds) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        if (userIds.length>0){
            GroupMember[] members = new GroupMember[userIds.length];
            for (int i = 0; i < userIds.length; i++) {
                GroupMember member =new GroupMember();
                member.setId(userIds[i]);
                members[i]=member;
            }
            GroupModel group = new GroupModel()
                    .setId(groupId)
                    .setMembers(members)
                    .setName(groupName);
            Result groupCreateResult = (Result)Group.create(group);
            System.out.println("group create result:  " + groupCreateResult.toString());
            map = AppUtil.objectToMap(groupCreateResult);
        }else{
            map.put("code", Const.ERROR_CODE);
        }
        return map;
    }

    @Override
    public Map inviteGroup(String groupId, String groupName, String... userIds) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        if (userIds.length>0){
            GroupMember[] members = new GroupMember[userIds.length];
            for (int i = 0; i < userIds.length; i++) {
                GroupMember member =new GroupMember();
                member.setId(userIds[i]);
                members[i]=member;
            }
            GroupModel group = new GroupModel()
                    .setId(groupId)
                    .setMembers(members)
                    .setName(groupName);
            Result groupInviteResult = (Result)rongCloud.group.invite(group);
            System.out.println("group create result:  " + groupInviteResult.toString());
            map = AppUtil.objectToMap(groupInviteResult);
        }else{
            map.put("code", Const.ERROR_CODE);
        }
        return map;
    }

    @Override
    public Map findMembers(String groupId) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        GroupModel group = new GroupModel().setId(groupId);
        GroupUserQueryResult getMemberesult = Group.get(group);
        System.out.println("group getMember:  " + getMemberesult.toString());
        map = AppUtil.objectToMap(getMemberesult);
        return map;
    }


    @Override
    public Map joinGroup(String groupId, String groupName, String... userIds) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        if (userIds.length>0){
            GroupMember[] members = new GroupMember[userIds.length];
            for (int i = 0; i < userIds.length; i++) {
                GroupMember member =new GroupMember();
                member.setId(userIds[i]);
                members[i]=member;
            }
            GroupModel group = new GroupModel()
                    .setId(groupId)
                    .setMembers(members)
                    .setName(groupName);
            Result groupJoinResult = (Result)Group.join(group);
            System.out.println("join:  " + groupJoinResult.toString());
            map = AppUtil.objectToMap(groupJoinResult);
        }else{
            map.put("code", Const.ERROR_CODE);
        }
        return map;
    }

    @Override
    public Map quitGroup(String groupId, String groupName, String... userIds) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        if (userIds.length>0){
            GroupMember[] members = new GroupMember[userIds.length];
            for (int i = 0; i < userIds.length; i++) {
                GroupMember member =new GroupMember();
                member.setId(userIds[i]);
                members[i]=member;
            }
            GroupModel group = new GroupModel()
                    .setId(groupId)
                    .setMembers(members)
                    .setName(groupName);
            Result groupQuitResult = (Result)Group.quit(group);
            System.out.println("quit:  " + groupQuitResult.toString());
            map = AppUtil.objectToMap(groupQuitResult);
        }else{
            map.put("code", Const.ERROR_CODE);
        }
        return map;
    }

    @Override
    public Map dismissGroup(String groupId, String groupName, String... userIds) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        if (userIds.length>0){
            GroupMember[] members = new GroupMember[userIds.length];
            for (int i = 0; i < userIds.length; i++) {
                GroupMember member =new GroupMember();
                member.setId(userIds[i]);
                members[i]=member;
            }
            GroupModel group = new GroupModel()
                    .setId(groupId)
                    .setMembers(members)
                    .setName(groupName);
            Result groupCreateResult = (Result)Group.dismiss(group);
            System.out.println("group create result:  " + groupCreateResult.toString());
            map = AppUtil.objectToMap(groupCreateResult);
        }else{
            map.put("code", Const.ERROR_CODE);
        }
        return map;
    }

    @Override
    public Map updateGroup(String groupId, String groupName) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        GroupModel group = new GroupModel()
                .setId(groupId)
                .setName(groupName);
        Result refreshResult = (Result)Group.update(group);
        System.out.println("refresh:  " + refreshResult.toString());
        map = AppUtil.objectToMap(refreshResult);
        return map;
    }


}
